package com.wit.action;

import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import hibernate.HibernateSessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import com.opensymphony.xwork2.ActionSupport;
import com.wit.po.Company;
import com.wit.po.Subcompany;
import com.wit.service.CompanyService;
import com.wit.service.SubcompanyService;
import com.wit.util.Pager;

@SuppressWarnings("serial")
public class SubCompanyAction extends ActionSupport{
	
	private String companyNum;
	
	private Subcompany subcompany;
	
	private SubcompanyService subcompanyService;
	
	private CompanyService companyService; 
	
	private List<Company> companyList;
	
	private List<Subcompany> subcompanyList;
	
	private String companynameC;
	
	public String getCompanynameC() {
		return companynameC;
	}

	public void setCompanynameC(String companynameC) {
		this.companynameC = companynameC;
	}

	private Pager pager;
	
	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public List<Company> getCompanyList() {
		return companyList;
	}

	public void setCompanyList(List<Company> companyList) {
		this.companyList = companyList;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public List<Subcompany> getSubcompanyList() {
		return subcompanyList;
	}

	public void setSubcompanyList(List<Subcompany> subcompanyList) {
		this.subcompanyList = subcompanyList;
	}

	public String getCompanyNum() {
		return companyNum;
	}

	public void setCompanyNum(String companyNum) {
		this.companyNum = companyNum;
	}

	public Subcompany getSubcompany() {
		return subcompany;
	}

	public void setSubcompany(Subcompany subcompany) {
		this.subcompany = subcompany;
	}

	public SubcompanyService getSubcompanyService() {
		return subcompanyService;
	}

	public void setSubcompanyService(SubcompanyService subcompanyService) {
		this.subcompanyService = subcompanyService;
	}
	
	@SuppressWarnings("unchecked")
	public String addSubcompany(){
		System.out.println("subcompany"+this.subcompany.getSubcommpanyId());
		subcompany.setCompany(companyService.findCompanyByIdenfier(companyNum));
		subcompany.setInsurancedefs(new HashSet(0));
		companyList = companyService.findAllCompany();
		if(subcompanyService.addSubcompany(subcompany))
			return "success";
		else
			return "input";
	}
	
	public String deleteSubcompany(){
		System.out.println("subcompany"+this.subcompany.getSubcommpanyId());
		if(subcompanyService.deleteSubcompany(subcompanyService.findSubcompanyByIdentifier(subcompany.getSubcommpanyId()))){
			subcompany.setSubcommpanyId(null);
			return "success";
		}else{
			subcompany.setSubcommpanyId(null);
			return "input";
		}
	}
	
	public String modifySubcompany(){
		System.out.println("subcompany"+this.subcompany.getSubcommpanyId());
		companyList = companyService.findAllCompany();
		subcompany.setCompany(companyService.findCompanyByIdenfier(companyNum));
		if(subcompanyService.modifySubcompany(subcompany))
			return "success";
		else
			return "input";
	}
	
	public String findSubcompanyByCompany() {
		System.out.println(companyNum);
		Company comp = companyService.findCompanyByIdenfier(companyNum);
		StringBuilder result = new StringBuilder("[");
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=gbk");
		response.setHeader("cache-control", "no-cache");
		if(comp != null){
			subcompanyList = subcompanyService.findSubcompanyByCompany(comp);
			for(int i = 0; i < subcompanyList.size(); i ++){
				result.append("{");
				result.append("insuranceNumber");
				result.append(":");
				result.append("\""+subcompanyList.get(i).getSubcommpanyId()+"\"");
				result.append(",");
				result.append("insuranceName");
				result.append(":");
				result.append("\""+subcompanyList.get(i).getSshortName()+"\"");
				result.append("}");
				if(i != subcompanyList.size() - 1)
					result.append(",");
			}
		}
		result.append("]");
		System.out.println(result.toString());
		try{
		PrintWriter out = response.getWriter();
		out.print(result);
		
		out.flush();
		out.close();
		}catch(Exception e){
			e.printStackTrace();
		} 
		return null;
	}

	public String findSubcompanyByCondition() {
		DetachedCriteria dc = DetachedCriteria.forClass(Subcompany.class);
		DetachedCriteria dcCount = DetachedCriteria.forClass(Subcompany.class);
		if(subcompany.getSubcommpanyId() != null && !subcompany.getSubcommpanyId().equals("")){
			dc.add(Property.forName("subcommpanyId").like("%"+subcompany.getSubcommpanyId()+"%"));
			dcCount.add(Property.forName("subcommpanyId").like("%"+subcompany.getSubcommpanyId()+"%"));
		}
		if(subcompany.getSfullName() != null && !subcompany.getSfullName().equals("")){
			dc.add(Property.forName("sfullName").like("%"+subcompany.getSfullName()+"%"));
			dcCount.add(Property.forName("sfullName").like("%"+subcompany.getSfullName()+"%"));
		}
		if(this.getCompanynameC() != null && !this.getCompanynameC().equals("")){
			dc.createAlias("company", "company");
			dcCount.createAlias("company", "company");
			dc.add(Property.forName("company.companynameC").like("%"+this.getCompanynameC()+"%"));
			dcCount.add(Property.forName("company.companynameC").like("%"+this.getCompanynameC()+"%"));
		}
		dcCount.setProjection(Property.forName("subcommpanyId").count());
		Integer count = subcompanyService.findSubcompanyCount(dcCount);
		pager.setRecordCount(count);
		pager.setPageCount(count%pager.getPageSize() == 0?count/pager.getPageSize():count/pager.getPageSize()+1);
		if(pager.getPageCurrent() == null || pager.getPageCurrent() > pager.getPageCount()){
			pager.setPageCurrent(1);
		}
		subcompanyList = subcompanyService.findSubcompanyByCondition(dc,(pager.getPageCurrent() - 1)*pager.getPageSize(),pager.getPageSize());
		System.out.println("current page"+pager.getPageCurrent());
		return "success";
	}
	
	public String findSubcompanyByIdentifier() {
		System.out.println(subcompany.getSubcommpanyId());
		companyList = companyService.findAllCompany();
		subcompany = subcompanyService.findSubcompanyByIdentifier(subcompany.getSubcommpanyId());
		System.out.println(subcompany);
		System.out.println(subcompany.getCompany());
		companyNum = subcompany.getCompany().getCompanyNum();
		return "success";
	}
}
